/*!
 * jQuery wgallery plugin v1.0
 *
 * Copyright 2012 Joe Wang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
(function($) {

  $.fn.wgallery = function(opt) {
    var options = $.extend({}, $.wgallery.defaults, opt);
    var w, h, navTop, navBottom, navLeft, navRight;
    var gallery = $(this);
    gallery.addClass("wgallery");
    if (options.datasource != null) {
      var html = '<div class="wgallery-list"><ul>';
      for (var i in options.datasource) {
        var description = options.datasource[i].description || "";
        html += '<li>';
        if (options.datasource[i].link) {
          html += '<a href="' + options.datasource[i].link + '">';
        }
        html += '<img src="'
        if (options.datasource[i].thumb) {
          html += options.datasource[i].thumb + '" bigsrc="';
        }
        html += options.datasource[i].image + '" alt="' + description + '" title="' + description + '" />';
        if (options.datasource[i].link) {
          html += '</a>';
        }
        html += '</li>';
      }
      html += "</ul></div>";
      gallery.html(html);
    } else {
      gallery.html('<div class="wgallery-list">' + gallery.html() + '</div>');
    }
    var list = gallery.find(".wgallery-list");
    var ul = gallery.find("ul");
    if (options.loop && ul.children().length > options.showImages) {
      ul.append(ul.html());
    }
    ul.find("img").attr({
      width: options.width,
      height: options.height
    });
  
    ul.find("li").css("padding", options.padding).mouseenter(function() {
      $(this).addClass("wgallery-current");
    }).mouseleave(function() {
      $(this).removeClass("wgallery-current");
    });
    if (options.isVertical) {
      w = options.width + options.padding * 2;
      h = (options.height + options.padding * 2) * options.showImages;
      ul.height(ul.children().length * (options.height + options.padding * 2)).width(w);
      if (options.showNav) {
        navTop = $('<div class="wgallery-nav-top" style="width: ' + w + 'px;"></div>');
        navBottom = $('<div class="wgallery-nav-bottom" style="width: ' + w + 'px;"></div>');
        list.before(navTop).after(navBottom);
        gallery.css({
          paddingTop: navTop.height(),
          paddingBottom: navBottom.height()
        });
        navTop.click(function() {
          $.wgallery.prev(options, gallery);
        });
        navBottom.click(function() {
          $.wgallery.next(options, gallery);
        });
      }
    } else {
      w = (options.width + options.padding * 2) * options.showImages;
      h = options.height + options.padding * 2;
      ul.width(ul.children().length * (options.width + options.padding * 2)).height(h);
      ul.children("li").css("float", "left");
      if (options.showNav) {
        navLeft = $('<div class="wgallery-nav-left" style="height: ' + h + 'px;"></div>');
        navRight = $('<div class="wgallery-nav-right" style="height: ' + h + 'px;"></div>');
        list.before(navLeft).before(navRight);
        navRight.css("margin-left", w);
        gallery.css({
          paddingLeft: navLeft.width(),
          paddingRight: navRight.width()
        });
        navLeft.click(function() {
          $.wgallery.prev(options, gallery);
        });
        navRight.click(function() {
          $.wgallery.next(options, gallery);
        });
      }
    }
    list.width(w).height(h);
    gallery.width(w).height(h).show();
  };
  
  $.wgallery = {
    defaults: {
      showNav: true,
      isVertical: true,
      width: 120,
      height: 90,
      padding: 5,
      showImages: 4,
      scrollImages: 4,
      speed: 500,
      datasource: null,
      loop: true
    },

    prev: function(options, gallery) {
      var scrollUnit = (options.isVertical ? options.height : options.width) + options.padding * 2;
	    var scrollSize = scrollUnit  * options.scrollImages;
      var list = gallery.find(".wgallery-list");
      var ul = gallery.find("ul");
      ul.stop();
	    if (options.isVertical) {
	      var marginTop = parseInt(ul.css("margin-top"));
        if (options.loop && ul.children("li").length > options.showImages && marginTop + ul.height() / 2 < 0) {
          marginTop += ul.height() / 2;
          ul.css("margin-top", marginTop);
        }
	      if (ul.height() - list.height() + marginTop > 0) {
	        var y = Math.ceil(marginTop / scrollUnit) * scrollUnit - scrollSize;
	        if (ul.height() - list.height() + y < 0) {
		        y = list.height() - ul.height();
	        }
	        ul.animate({marginTop: y}, options.speed);
	      }
	    } else {
        var marginLeft = parseInt(ul.css("margin-left"));
        if (options.loop && ul.children("li").length > options.showImages && marginLeft + ul.width() / 2 < 0) {
          marginLeft += ul.width() / 2;
          ul.css("margin-left", marginLeft);
        }
        if (ul.width() - list.width() + marginLeft > 0) {
	        var x = Math.ceil(marginLeft / scrollUnit) * scrollUnit - scrollSize;
	        if (ul.width() - list.width() + x < 0) {
		        x = list.width() - ul.width();
	        }
	        ul.animate({marginLeft: x}, options.speed);
	      }
      }
    },
  
    next: function(options, gallery) {
      var scrollUnit = (options.isVertical ? options.height : options.width) + options.padding * 2;
	    var scrollSize = scrollUnit  * options.scrollImages;
      var ul = gallery.find("ul");
      ul.stop();
	    if (options.isVertical) {
	      var marginTop = parseInt(ul.css("margin-top"));
        if (options.loop && ul.children("li").length > options.showImages && marginTop + ul.height() / 2 > 0) {
          marginTop -= ul.height() / 2;
          ul.css("margin-top", marginTop);
        }
	      if (marginTop < 0) {
	        var y = Math.ceil(marginTop / scrollUnit) * scrollUnit + scrollSize;
	        if (y > 0) {
		        y = 0;
	        }
	        ul.animate({marginTop: y}, options.speed);
	      }
	    } else {
        var marginLeft = parseInt(ul.css("margin-left"));
        if (options.loop && ul.children("li").length > options.showImages && marginLeft + ul.width() / 2 > 0) {
          marginLeft -= ul.width() / 2;
          ul.css("margin-left", marginLeft);
        }
        if (marginLeft < 0) {
	        var x = Math.ceil(marginLeft / scrollUnit) * scrollUnit + scrollSize;
	        if (x > 0) {
		        x = 0;
	        }
	        ul.animate({marginLeft: x}, options.speed);
	      }
      }
    }
  };

})(jQuery);